package com.iris.live.services.data.repositories.impl;

import com.iris.live.services.data.repositories.CustomerInfoRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/9.
 */
@Repository
@Transactional//(readOnly = true)
public class CustomerInfoRepositoryImpl extends BaseRepository implements CustomerInfoRepository {

    @Override
    public List mateCarTypeForILogCar(String carType) {
        String sql = "select * from ilog_car a WHERE a.type = :carType ";
        Map param = new HashMap();
        param.put("carType", carType);
        return this.getList(sql, param);
    }

    @Override
    public List getCarId () {
        String sql = "select * from ilog_car a";
        return this.getList(sql);
    }

    public List getLshLeadingIn (String permit, Date date) {
        String sql = "select a.许可,a.电话号码 from lsh导入名单 a where a.许可 = :permit and a.导入日期 >= :date and a.是否有效线索 = '是' and a.是否线索 = '是'";
        Map param = new HashMap();
        param.put("permit", permit);
        param.put("date", date);
        return this.getList(sql, param);
    }

    public List getSCInfo (String permit) {
        String sql = "select a.许可, a.用户名, a.人员姓名 from 用户 a where a.许可 = :permit and (a.删除状态 <> '已删除' or a.删除状态 is null) and (a.销售顾问职位状态 <> '离职' or a.销售顾问职位状态 is null)";
        Map param = new HashMap();
        param.put("permit", permit);
        return this.getList(sql, param);
    }

    public List getCusInfo (String permit) {
        String sql = "select a.顾客编号, a.电话号码 from 顾客记录汇总 a where a.许可 = :permit";
        Map param = new HashMap();
        param.put("permit", permit);
        return this.getList(sql, param);
    }


    public List getBjdMax ( String gukeNo,String ishyd,String isbh) {
        String sql = "SELECT a.* FROM (SELECT * FROM 报价单表  ORDER BY 报价编号 DESC ) a WHERE a.顾客编号 =:gukeNo   ";
        if(StringUtils.isNotBlank(ishyd)){
            sql+=" AND a.是否还原点='"+ishyd+"' ";
        }if(StringUtils.isNotBlank(isbh)){
            sql+=" AND a.是否驳回='"+isbh+"' ";
        }

        sql+="  GROUP BY a.初始报价单号 ";
        Map param = new HashMap();
        param.put("gukeNo", gukeNo);
        return this.getList(sql, param);
    }

    /**
     * 获取最大报价编号的记录
     * @param customerNo
     * @param ishyd
     * @param isbh
     * @return
     */
    public List getMaxQuotationNo(String customerNo, String ishyd, String isbh){
        String sql="SELECT * FROM 报价单表 a WHERE a.顾客编号 =:customerNo  ";

        if(StringUtils.isNotBlank(ishyd)){
            sql+=" AND a.是否还原点='"+ishyd+"' ";
        }if(StringUtils.isNotBlank(isbh)){
            sql+=" AND a.是否驳回='"+isbh+"' ";
        }
        sql+=" ORDER BY a.报价编号 DESC LIMIT 1";
        Map param = new HashMap();
        param.put("customerNo", customerNo);
        return this.getList(sql, param);

    }

    public List getUnpaidCar(Integer bianhao){
        String sql = "SELECT a.库存编号,a.初始报价单号,a.销售顾问,a.顾客编号 FROM  `报价单表` a LEFT JOIN  `报价交车表` b ON a.交车=b.编号 WHERE a.报价编号=:bianhao AND b.交车日期 IS NULL   ";
        Map param = new HashMap();
        param.put("bianhao", bianhao);
        return this.getList(sql, param);
    }

    @Override
    public Map<String, Object> getCustomerInfo(String quotationId) {
        StringBuffer sb = new StringBuffer();

        sb.append(" select ");
        return null;
    }
}
